package com.web.boot.web.auth;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.facade.MenuFacade;
import com.web.base.auth.message.AppResponse;
import com.web.base.auth.message.MenuRequest;
import com.web.base.auth.message.MenuResponse;
import com.web.boot.global.model.App;
import com.web.boot.global.util.HttpContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

@Controller("authMenuController")
@Http(url = "/auth")
public class MenuController
{
	@Autowired
	private MenuFacade menuFacade;
	@Resource
	private AppFacade appFacade;
	@Resource
	private HttpContext httpContext;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public MenuController()
	{
	}
	
	@Http(url = "/menu_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		MenuRequest menuRequest = request.getQueryObject(MenuRequest.class);
		List<AppResponse> appList = appFacade.listAll();
		AppResponse app = null;
		if (menuRequest.getAppId() == null && !appList.isEmpty())
		{
			app = appList.get(0);
			menuRequest.setAppId(app.getId());
		}
		else if (menuRequest.getAppId() != null)
		{
			app = appFacade.get(menuRequest.getAppId());
		}
		httpContext.setApp(request, App.toApp(app));

		return View.createBuilder().setView("/auth/menu_list")
				.put("appList", appList)
				.put("app", app)
				.put("model", request)
				.put("map", dictParser.getStringMap())
				.put("treeList", menuFacade.list(menuRequest))
				.build();
	}
	
	@Http(url = "/menu_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		MenuRequest menuRequest = request.getQueryObject(MenuRequest.class);
		MenuResponse response = null;
		App app = httpContext.getApp(request);
		if (menuRequest.getId() != null)
		{
			response = menuFacade.get(menuRequest.getId());
		}
		else
		{
			response = new MenuResponse();
			response.setParentId(menuRequest.getParentId());
		}
		ViewBuilder builder = View.createBuilder();
		if (response.getParentId() != null)
		{
			builder.put("parentModel", menuFacade.get(response.getParentId()));
		}
		MenuRequest search = new MenuRequest();
		search.setAppId(app.getId());
		return builder.setView("/auth/menu_get")
				.put("treeList", menuFacade.list(search))
				.put("app", app)
				.put("model", response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/menu_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		MenuRequest menuRequest = request.getPostObject(MenuRequest.class);
		MenuResponse response = null;
		if (menuRequest.getId() == null)
		{
			ValidationUtil.valid(menuRequest, SaveModel.class);
			response = menuFacade.add(menuRequest);
		}
		else
		{
			ValidationUtil.valid(menuRequest, UpdateModel.class);
			response = menuFacade.update(menuRequest);
		}
	}
	
	@Http(url = "/menu_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		menuFacade.refresh();
	}

	@Http(url = "/menu_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		int id = request.getPostInt("id", 0);
		menuFacade.delete(id);
	}
	
}
